package com.luoxiang.socialandroid.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.luoxiang.socialandroid.R;

public class AlertDialogGreet {
    private static View view_custom;
    public static AlertDialog.Builder builder;
    public static AlertDialog alert;
    private static EditText edit_greet;
    private static TextView tx_edit_size;
    private static TextView add;

    public static AlertDialogGreet getInstance() {
        return new AlertDialogGreet();
    }

    public static void setTitle(String text) {
        add.setText(text);
    }

    public void setSize(String text) {
        tx_edit_size.setText(text);
    }


    public static void showConfirmDialog(Context context, String text) {
        builder = new AlertDialog.Builder(context);
        alert = builder.create();
        alert.show();

        alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        //引入布局
        view_custom = LayoutInflater.from(context).inflate(R.layout.alert_dialog_greet, null, false);
        alert.setCancelable(false); //点击空白处不关闭弹窗
        edit_greet = view_custom.findViewById(R.id.edit_greet);
        edit_greet.setText(text);
        tx_edit_size = view_custom.findViewById(R.id.tx_edit_size);
        add = view_custom.findViewById(R.id.add);
        edit_greet.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                tx_edit_size.setText(String.valueOf(edit_greet.getText().length()));
            }
        });
        //为取消按钮设置点击监听
        view_custom.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDialogButtonClickListener != null) {
                    mOnDialogButtonClickListener.onNegativeButtonClick(alert);
                }
            }
        });
        //为确认按钮设置点击监听
        view_custom.findViewById(R.id.comfirm_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDialogButtonClickListener != null) {
                    mOnDialogButtonClickListener.onPositiveButtonClick(alert, edit_greet.getText().toString());
                }
            }
        });
        //使用布局
        alert.getWindow().setContentView(view_custom);
    }

    //todo 按钮点击回调接口
    public static AlertDialogGreet.OnDialogButtonClickListener mOnDialogButtonClickListener;

    public void setDialogButtonClickListener(AlertDialogGreet.OnDialogButtonClickListener listener) {
        this.mOnDialogButtonClickListener = listener;
    }

    public interface OnDialogButtonClickListener {
        void onPositiveButtonClick(AlertDialog dialog, String text); //确认

        void onNegativeButtonClick(AlertDialog dialog); //取消
    }

}
